<?php
namespace app\controller\handler;
use app\controller\Common;
use think\Db;
use think\Model;

class News extends Common{

  public function getNewsList(){
    $base = input('post.');
    $page = isset($base['page']) ? trim($base['page']): '';
    $pagesize = isset($base['pagesize']) ? trim($base['pagesize']): '';
    $current = ($page-1)*$pagesize;
    $sql = "select n.*
            from `#__news` n
            where n.status=1 order by n.id desc limit ?,?
          ";
    $countSql = "select count(*) as total
            from `#__news` n
            where n.status=1 order by n.id desc
          ";
    $res = Db::query($sql,[$current,$pagesize]);
    $count = Db::query($countSql);
    if($res && count($res)>0){
      $result['ret'] = 1;
      $result['data'] = $res;
      $result['total'] = $count[0]['total'];
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function addNews(){
    $base = input('post.')['base'];
    $base['ctime'] = time();
    $res = model('News')::create($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '添加成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '添加失败';
    }
    return $result;
  }

  public function getNewsDetailById(){
    $wh['id'] = input('post.')['id'];
    $wh['status'] = 1;
    $res = model('News')::get($wh);
    if($res){
      $result['ret'] = 1;
      $result['data'] = $res;
    }else{
      $result['ret'] = 0;
      $result['msg'] = '未查询到数据';
    }
    return $result;
  }

  public function editNews(){
    $base = input('post.')['base'];
    $wh['id'] = input('post.')['id'];
    $res = model('News')::where($wh)->update($base);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '编辑成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '编辑失败';
    }
    return $result;
  }

  public function deleteNews(){
    $wh['id'] = input('post.')['id'];
    $up['status'] = 0;
    $res = model('News')::where($wh)->update($up);
    if($res){
      $result['ret'] = 1;
      $result['msg'] = '删除成功';
    }else{
      $result['ret'] = 0;
      $result['msg'] = '删除失败';
    }
    return $result;
  }



}//END